<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" @click="table_add"></el-button>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" :apiObj="apiObj" row-key="id" stripe>
                <sc-table-column label="序号" width="65" type="index"/>
                <el-table-column label="运营参数编码" prop="configCode" width="200"></el-table-column>
                <el-table-column label="运营参数名称" prop="configName" width="300"></el-table-column>
                <el-table-column label="运营参数值" prop="configValue" width="200"></el-table-column>
                <el-table-column label="运营参数备注" prop="configNote" min-width="250"></el-table-column>
                <el-table-column label="状态" prop="status" width="80">
                    <template #default="scope">
                        <el-switch v-model="scope.row.status" @change="changeSwitch($event, scope.row)" :loading="scope.row.$switch_status"
                                   active-value="ENABLED" inactive-value="DISABLED"></el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" prop="createTime" width="180"></el-table-column>
                <el-table-column label="操作" fixed="right" width="150">
                    <template #default="scope">
                        <el-button-group>
                            <el-button type="info" size="small" @click="table_show(scope.row, scope.$index)" icon="el-icon-view"></el-button>
                            <el-button type="primary" size="small" @click="table_edit(scope.row, scope.$index)" icon="el-icon-edit"></el-button>
                            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button type="danger" size="small" icon="el-icon-delete"></el-button>
                                </template>
                            </el-popconfirm>
                        </el-button-group>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>

    <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>

</template>

<script>
import saveDialog from './save'

export default {
    name: "client",
    components: {
        saveDialog
    },
    data() {
        return {
            dialog: {
                save: false
            },
            apiObj: this.$API.system.config,
        }
    },
    methods: {
        //查看
        table_show(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('show').setData(row)
            })
        },
        //增加
        table_add() {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open()
            })
        },
        //编辑
        table_edit(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('edit').setData(row)
            })
        },
        //表格内开关事件
        async changeSwitch(val, row) {
            //1.还原数据
            row.status = row.status === 'ENABLED' ? 'DISABLED' : 'ENABLED'
            //2.执行加载
            row.$switch_status = true;
            //3.等待接口返回后改变值
            const res = await this.$API.system.config.switch({id: row.id, status: val});
            if (res.code === 200) {
                delete row.$switch_status;
                row.status = val;
                this.$message.success("操作成功")
            } else {
                await this.$alert(res.msg, "提示", {type: 'error'})
            }
        },
        //删除
        async table_del(row, index) {
            const res = await this.$API.system.config.delete(row.id);
            if (res.code === 200) {
                //这里选择刷新整个表格 OR 插入/编辑现有表格数据
                this.$refs.table.tableData.splice(index, 1);
                this.$message.success("删除成功")
            } else {
                await this.$alert(res.msg, "提示", {type: 'error'})
            }
        },
        //本地更新数据
        handleSuccess(data, mode) {
            if (mode === 'add') {
                data.id = new Date().getTime()
                this.$refs.table.tableData.unshift(data)
            } else if (mode === 'edit') {
                this.$refs.table.tableData.filter(item => item.id === data.id).forEach(item => {
                    Object.assign(item, data)
                })
            }
        }
    }
}
</script>

<style>
</style>
